#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <jsoncpp/json/json.h>
std::string serialize()
{
    //1.数据存到value中
    Json::Value root;
    root["name"]="liu";
    root["age"]=18;
    root["score"].append(99);
    root["score"].append(88);
    //2.创建bulider
    Json::StreamWriterBuilder swb;
    //3.创建writer
    Json::StreamWriter *sw=swb.newStreamWriter();
    //4.序列化
    std::stringstream ss;
    int ret=sw->write(root,&ss);
    if(ret<0)
    {
        std::cout<<"序列化出错"<<std::endl;
        delete sw;
        return "";
    }

    std::string result=ss.str();
    std::cout<<result<<std::endl;
    delete sw;
    return ss.str();

}
void  unserialize(std::string str)
{
    //1.创建builder
    Json::CharReaderBuilder crb;
    //2.创建reader
    Json::CharReader *cr=crb.newCharReader();
    //3.创建value
    Json::Value root;
    //4.反序列化
    std::string err;
   bool ret= cr->parse(str.c_str(),str.c_str()+str.size(),&root,&err);
   if(!ret)
   {
        std::cout<<"反序列化出错"<<std::endl;
        return ;
   }
    //5.访问数据
    std::cout<<"name: "<<root["name"].asString()<<std::endl;
     std::cout<<"age: "<<root["age"].asInt()<<std::endl;
     int sz=root["score"].size();
     for(int i=0;i<sz;i++)
     {
        std::cout<<"score: "<<root["socre"][i].asFloat()<<std::endl;
     }





}
int main()
{
    std::string str = serialize();
    unserialize(str);
    return 0;
}